package com.qf.lja.servlet;

import com.qf.lja.dao.UserDao;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet(urlPatterns = "/UserDel.do")
public class UserDel extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        String user_id = req.getParameter("user_id");

        if (user_id == null || user_id.isEmpty()) {
            resp.sendError(400, "缺少用户ID");
            return;
        }
        int userIdInt = Integer.parseInt(user_id);
        UserDao userDao = new UserDao();
        boolean success = userDao.deleteUserById(userIdInt);

        if (success) {
            resp.sendRedirect(req.getContextPath() + "/UserList.do");
        } else {
            resp.sendError(500, "删除用户失败");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // 如果需要在 doPost 方法中处理删除操作，可以根据具体需求进行实现
        String user_id = req.getParameter("user_id");

        if (user_id == null || user_id.isEmpty()) {
            resp.sendError(400, "缺少用户ID");
            return;
        }
        int userIdInt = Integer.parseInt(user_id);
        UserDao userDao = new UserDao();
        boolean success = userDao.deleteUserById(userIdInt);

        if (success) {
            resp.sendRedirect(req.getContextPath() + "/UserList.do");
        } else {
            resp.sendError(500, "删除用户失败");
        }
    }
}